home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / asm / mylib.asm < prev    next >
Assembly Source File  |  1986-01-15  |  9KB  |  343 lines

  1.  
  2. * mylib.asm AmigaLink 1/30/86
  3. *************************************************************************
  4. *                                            *
  5. *    Copyright (C) 1985, Commodore Amiga Inc.  All rights reserved.   *
  6. *                                            *
  7. ************************************************************************/
  8.  
  9.  
  10. *************************************************************************
  11. *
  12. * mylib.asm -- skeleton library code
  13. *
  14. * Source Control
  15. * ------ -------
  16. * $Header: amain.asm,v 31.3 85/10/18 19:04:04 neil Exp $
  17. *
  18. * $Locker: neil $
  19. *
  20. * $Log:   amain.asm,v $
  21. *
  22. ************************************************************************/
  23.      SECTION   section
  24.  
  25.      NOLIST
  26.      include "exec/types.i"
  27.      include "exec/nodes.i"
  28.      include "exec/lists.i"
  29.      include "exec/libraries.i"
  30.      include "exec/alerts.i"
  31.      include "exec/initializers.i"
  32.      include "exec/resident.i"
  33.      include "libraries/dos.i"
  34.  
  35.      include "asmsupp.i"
  36.  
  37.      include "mylib.i"
  38.  
  39.      LIST
  40.  
  41.      ;------ These don't have to be external, but it helps some
  42.      ;------ debuggers to have them globally visible
  43.      XDEF Init
  44.      XDEF Open
  45.      XDEF Close
  46.      XDEF Expunge
  47.      XDEF Null
  48.      XDEF myName
  49.      XDEF MyFunc0
  50.      XDEF MyFunc1
  51.  
  52.      XREF _AbsExecBase
  53.  
  54.      XLIB OpenLibrary
  55.      XLIB CloseLibrary
  56.      XLIB Alert
  57.      XLIB FreeMem
  58.      XLIB Remove
  59.  
  60.  
  61.  
  62.      ; The first executable location.  This should return an error
  63.      ; in case someone tried to run you as a program (instead of
  64.      ; loading you as a library).
  65. Start:
  66.      CLEAR     d0
  67.      rts
  68.  
  69. ;-----------------------------------------------------------------------
  70. ; A romtag structure.  Both "exec" and "ramlib" look for
  71. ; this structure to discover magic constants about you
  72. ; (such as where to start running you from...).
  73. ;-----------------------------------------------------------------------
  74.  
  75.      ; Most people will not need a priority and should leave it at zero.
  76.      ; the RT_PRI field is used for configuring the roms.  Use "mods" from
  77.      ; wack to look at the other romtags in the system
  78. MYPRI     EQU  0
  79.  
  80. initDDescrip:
  81.                          ;STRUCTURE RT,0
  82.        DC.W    RTC_MATCHWORD       ; UWORD RT_MATCHWORD
  83.        DC.L    initDDescrip        ; APTR  RT_MATCHTAG
  84.        DC.L    EndCode        ; APTR  RT_ENDSKIP
  85.        DC.B    RTF_AUTOINIT        ; UBYTE RT_FLAGS
  86.        DC.B    VERSION        ; UBYTE RT_VERSION
  87.        DC.B    NT_LIBRARY          ; UBYTE RT_TYPE
  88.        DC.B    MYPRI               ; BYTE  RT_PRI
  89.        DC.L    myName         ; APTR  RT_NAME
  90.        DC.L    idString       ; APTR  RT_IDSTRING
  91.        DC.L    Init           ; APTR  RT_INIT
  92.                          ; LABEL RT_SIZE
  93.  
  94.  
  95.      ; this is the name that the library will have
  96. myName:        MYLIBNAME
  97.  
  98.      ; a major version number.
  99. VERSION:  EQU  1
  100.  
  101.      ; A particular revision.  This should uniquely identify the bits in the
  102.      ; library.  I use a script that advances the revision number each time
  103.      ; I recompile.  That way there is never a question of which library
  104.      ; that really is.
  105. REVISION: EQU  17
  106.  
  107.      ; this is an identifier tag to help in supporting the library
  108.      ; format is 'name version.revision (dd MON yyyy)',<cr>,<lf>,<null>
  109. idString: dc.b 'mylib 1.0 (31 Oct 1985)',13,10,0
  110.  
  111. dosName:  DOS`00`00u`A4
  112.  
  113.      ; force word allignment
  114.      ds.w 0
  115.  
  116.  
  117.      ; The romtag specified that we were "RTF_AUTOINIT".  This means
  118.      ; that the RT_INIT structure member points to one of these
  119.      ; tables below.  If the AUTOINIT bit was not set then RT_INIT
  120.      ; would point to a routine to run.
  121.  
  122. Init:
  123.      DC.L MyLib_Sizeof        ; data space size
  124.      DC.L funcTable      ; pointer to function initializers
  125.      DC.L dataTable      ; pointer to data initializers
  126.      DC.L initRoutine         ; routine to run
  127.  
  128.  
  129. funcTable:
  130.  
  131.      ;------ standard system routines
  132.      dc.l Open
  133.      dc.l Close
  134.      dc.l Expunge
  135.      dc.l Null
  136.  
  137.      ;------ my libraries definitions
  138.      dc.l MyFunc0
  139.      dc.l MyFunc1
  140.  
  141.      ;------ function table end marker
  142.      dc.l -1
  143.  
  144.  
  145.      ; The data table initializes static data structures.
  146.      ; The format is specified in exec/InitStruct routine's
  147.      ; manual pages.  The INITBYTE/INITWORD/INITLONG routines
  148.      ; are in the file "exec/initializers.i".  The first argument
  149.      ; is the offset from the library base for this byte/word/long.
  150.      ; The second argument is the value to put in that cell.
  151.      ; The table is null terminated
  152. dataTable:
  153.      INITBYTE  LH_TYPE,NT_LIBRARY
  154.      INITLONG  LN_NAME,myName
  155.      INITBYTE  LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  156.      INITWORD  LIB_VERSION,VERSION
  157.      INITWORD  LIB_REVISION,REVISION
  158.      INITLONG  LIB_IDSTRING,idString
  159.      DC.L 0
  160.  
  161.  
  162.      ; This routine gets called after the library has been allocated.
  163.      ; The library pointer is in D0.  The segment list is in A0.
  164.      ; If it returns non-zero then the library will be linked into
  165.      ; the library list.
  166. initRoutine:
  167.  
  168.      ;------ get the library pointer into a convenient A register
  169.      move.l    a5,-(sp)
  170.      move.l    d0,a5
  171.  
  172.      ;------ save a pointer to exec
  173.      move.l    a6,ml_SysLib(a5)
  174.  
  175.      ;------ save a pointer to our loaded code
  176.      move.l    a0,ml_SegList(a5)
  177.  
  178.      ;------ open the dos library
  179.      lea  dosName(pc),a1
  180.      CLEAR     d0
  181.      CALLSYS   OpenLibrary
  182.  
  183.      move.l    d0,ml_DosLib(a5)
  184.      bne.s     1$
  185.  
  186.      ;------ can't open the dos!  what gives
  187.      ALERT     AG_OpenLib!AO_DOSLib
  188.  
  189. 1$:
  190.      ;------ now build the static data that we need
  191.  
  192.      ;
  193.      ; put your initialization here...
  194.      ;
  195.  
  196.      move.l    a5,d0
  197.      move.l    (sp)+,a5
  198.      rts
  199.  
  200. ;----------------------------------------------------------------------
  201. ;
  202. ; here begins the system interface commands.  When the user calls
  203. ; OpenLibrary/CloseLibrary/RemoveLibrary, this eventually gets translated
  204. ; into a call to the following routines (Open/Close/Expunge).  Exec
  205. ; has already put our library pointer in A6 for us.  Exec has turned
  206. ; off task switching while in these routines (via Forbid/Permit), so
  207. ; we should not take too long in them.
  208. ;
  209. ;----------------------------------------------------------------------
  210.  
  211.  
  212.      ; Open returns the library pointer in d0 if the open
  213.      ; was successful.  If the open failed then null is returned.
  214.      ; It might fail if we allocated memory on each open, or
  215.      ; if only open application could have the library open
  216.      ; at a time...
  217.  
  218. Open:          ; ( libptr:a6, version:d0 )
  219.  
  220.      ;------ mark us as having another opener
  221.      addq.w    #1,LIB_OPENCNT(a6)
  222.  
  223.      ;------ prevent delayed expunges
  224.      bclr #LIBB_DELEXP,ml_Flags(a6)
  225.  
  226.      move.l    a6,d0
  227.      rts
  228.  
  229.      ; There are two different things that might be returned from
  230.      ; the Close routine.  If the library is no longer open and
  231.      ; there is a delayed expunge then Close should return the
  232.      ; segment list (as given to Init).  Otherwise close should
  233.      ; return NULL.
  234.  
  235. Close:         ; ( libptr:a6 )
  236.     
  237.      ;------ set the return value
  238.      CLEAR     d0
  239.  
  240.      ;------ mark us as having one fewer openers
  241.      subq.w    #1,LIB_OPENCNT(a6)
  242.  
  243.      ;------ see if there is anyone left with us open
  244.      bne.s     1$
  245.  
  246.      ;------ see if we have a delayed expunge pending
  247.      btst #LIBB_DELEXP,ml_Flags(a6)
  248.      beq.s     1$
  249.  
  250.      ;------ do the expunge
  251.      bsr  Expunge
  252. 1$:
  253.      rts
  254.  
  255.  
  256.      ; There are two different things that might be returned from
  257.      ; the Expunge routine.  If the library is no longer open
  258.      ; then Expunge should return the segment list (as given to
  259.      ; Init).  Otherwise Expunge should set the delayed expunge
  260.      ; flag and return NULL.
  261.      ;
  262.      ; One other important note: because Expunge is called from
  263.      ; the memory allocator, it may NEVER Wait() or otherwise
  264.      ; take long time to complete.
  265.  
  266. Expunge:  ; ( libptr: a6 )
  267.  
  268.      movem.l   d2/a5/a6,-(sp)
  269.      move.l    a6,a5
  270.      move.l    ml_SysLib(a5),a6
  271.      
  272.      ;------ see if anyone has us open
  273.      tst.w     LIB_OPENCNT(a5)
  274.      beq  1$
  275.  
  276.      ;------ it is still open.  set the delayed expunge flag
  277.      bset #LIBB_DELEXP,ml_Flags(a5)
  278.      CLEAR     d0
  279.      bra.s     Expunge_End
  280.  
  281. 1$:
  282.      ;------ go ahead and get rid of us.  Store our seglist in d2
  283.      move.l    ml_SegList(a5),d2
  284.  
  285.      ;------ unlink from library list
  286.      move.l    a5,a0
  287.      CALLSYS   Remove
  288.      
  289.      ;
  290.      ; device specific closings here...
  291.      ;
  292.  
  293.      ;------ close the dos library
  294.      move.l    ml_DosLib(a5),a1
  295.      CALLSYS   CloseLibrary
  296.  
  297.      ;------ free our memory
  298.      move.l    a5,a1
  299.      move.l    LIB_NEGSIZE(a5),d0
  300.  
  301.      sub.l     d0,a1
  302.      add.l     LIB_POSSIZE(a5),d0
  303.  
  304.      CALLSYS   FreeMem
  305.  
  306.      ;------ set up our return value
  307.      move.l    d2,d0
  308.  
  309. Expunge_End:
  310.      movem.l   (sp)+,d2/a5/a6
  311.      rts
  312.  
  313.  
  314. Null:
  315.      CLEAR     d0
  316.      rts
  317.  
  318. ;----------------------------------------------------------------------
  319. ;
  320. ; here begins the library specific commands
  321. ;
  322. ;----------------------------------------------------------------------
  323.  
  324. MyFunc0:
  325.      CLEAR     d0
  326.      rts
  327.  
  328. MyFunc1:
  329.      moveq     #1,d0
  330.      rts
  331.  
  332.  
  333.      ; EndCode is a marker that show the end of your code.
  334.      ; Make sure it does not span sections nor is before the
  335.      ; rom tag in memory!  It is ok to put it right after
  336.      ; the rom tag -- that way you are always safe.  I put
  337.      ; it here because it happens to be the "right" thing
  338.      ; to do, and I know that it is safe in this case.
  339. EndCode:
  340.  
  341.      END
  342.